package com.express.util;

import java.text.SimpleDateFormat;
import java.util.*;

public class RandomNumber {
	
	private static long orderNum = 0;
	private static String date  ;
	private static String prefix = "LBT";
	
	/**
	 * 获取3位随机数据,并且不重复
	 * @return
	 */
	public static Set<Integer> getRandomNumber(){
		//使用Set 保证数据不重复
		Set<Integer> set = new HashSet<Integer>();
		//随机数
		Random r = new Random();
		while (set.size() < 3 ) {
			//添加随机数
			set.add(r.nextInt(10));
		}
		return set;
	}
	/** 
     * 随机生成6位数字作为验证码 
     * @return 
     */  
    public static String getCode(){  
        int code=(int)(Math.random()*9000+100000);  
        return code + "";  
    }
	public static String getCodeOne(){
		int code=(int)(Math.random()*10010+10000);
		return code + "";
	}
	/**
	 * 根据定义长度生成字符串随机数
	 * @param length
	 * @return
	 */
	public static String getRandomChar(int length) {
		char[] chr = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d',
				'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
				'u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','l','M',
				'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
		};
		
		Random r = new Random();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < length; i++) {
			buf.append(chr[r.nextInt(62)]);
		}
		return buf.toString();
	}
	/**
	 * 生成订单号
	 * @return
	 */
	public static synchronized String getOrderNo(String first,String area,String suffix) {
		Date newdate = new Date();
		Set<Integer> set = getRandomNumber();
		Iterator<Integer> iter = set.iterator();
		String no = "";//临时记录
		while(iter.hasNext()) {
			no += iter.next();
		}
		String str = new SimpleDateFormat("yyyy").format(newdate);
		String str1 = getCodeOne();
		//判断是否为空 或是不相同
		int number = Integer.parseInt(no);
		if(date == null || !date.equals(str)){
			date = str1;
			orderNum = number==0?orderNum:number ;
		}
		//规则8XNYBYYYYMMDDHHMMSS000000001
		orderNum ++;
//		long orderNo =  Long.parseLong((date));
		long orderNo =  Long.parseLong((date)) + number;
		return  first+str+area+orderNo+"";
	}

	/**
	 * 生成订单号
	 * @return
	 */
	public static synchronized String getOrderNo(String first,String area) {
		Date newdate = new Date();
		Set<Integer> set = getRandomNumber();
		Iterator<Integer> iter = set.iterator();
		String no = "";//临时记录
		while(iter.hasNext()) {
			no += iter.next();
		}
		String str = new SimpleDateFormat("yyyyMMdd").format(newdate);
		String str1 = new SimpleDateFormat("s").format(newdate);
		//判断是否为空 或是不相同
		int number = Integer.parseInt(no);
		if(date == null || !date.equals(str)){
			date = str1;
			orderNum = number==0?orderNum:number ;
		}
		//规则8XNYBYYYYMMDDHHMMSS000000001
		orderNum ++;
//		long orderNo =  Long.parseLong((date));
		long orderNo =  Long.parseLong((date)) + number;
		return  first+str+area+orderNo+"";
	}
	/**
	 * 生成订单号
	 * @return
	 */
	public static synchronized String getOrderNo(String area) {
		Date newdate = new Date();
		Set<Integer> set = getRandomNumber();
		Iterator<Integer> iter = set.iterator();
		String no = "";//临时记录
		while(iter.hasNext()) {
			no += iter.next();
		}
		String str = new SimpleDateFormat("yyyyMMddHHmmss").format(newdate);
		String str1 = new SimpleDateFormat("ss").format(newdate);
		//判断是否为空 或是不相同
		int number = Integer.parseInt(no);
		if(date == null || !date.equals(str)){
			date = str1;
			orderNum = number==0?orderNum:number ;
		}
		//规则8XNYBYYYYMMDDHHMMSS000000001 202510100
		orderNum ++;
//		long orderNo =  Long.parseLong((date));
		long orderNo =  Long.parseLong((date)) + number;
		return  str+area+orderNo+"";
	}
	
	public static void main(String[] args) {
		
		Set<Integer> set = getRandomNumber();
		
		Iterator<Integer> iter = set.iterator();
		String temp = "";//临时记录
		while(iter.hasNext()) {
			temp += iter.next();
		}
		System.out.println("3位随机数为:"+temp);
		//获取订单号
//		for (int i = 0; i < 10; i++) {
//			System.out.println(prefix + getOrderNo(1));
//			try {
//				//休息一秒
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
//		System.out.println("6位数据为:"+getRandomChar(6));
		System.out.println("temp="+temp);
		String res = getOrderNo("G");
		System.out.println(res.length()+","+res);
		System.out.println("LBT20200910161210G005".length());
		System.out.println(getOrderNo("lbt",""));
		System.out.println(getCodeOne());
		System.out.println(getCode());
	}
}
